package com.chenglu.android.logistics;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chenglu.android.R;
import com.chenglu.android.common.base.BaseActionBarActivity;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.SPUtils;
import com.chenglu.android.logistics.adapter.LogisticsAdapter;
import com.chenglu.android.logistics.bean.LogisticsBean;
import com.chenglu.android.logistics.bean.LogisticsData;
import com.chenglu.android.logistics.mvp.contract.LogisticsContract;
import com.chenglu.android.logistics.mvp.presenter.LogisticsPresenter;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author: lyj
 * @Created: by Copyright 2023 10/17 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：
 */
public class LogisticsActivity extends BaseActionBarActivity<LogisticsPresenter> implements LogisticsContract.View {
    @BindView(R.id.iv_order_goodsImg)
    ImageView ivOrderGoodsImg;
    @BindView(R.id.cv_order_goosImg)
    CardView cvOrderGoosImg;
    @BindView(R.id.tv_order_firm)
    TextView tvOrderFirm;
    @BindView(R.id.tv_order_no)
    TextView tvOrderNo;
    @BindView(R.id.rv_logistics)
    RecyclerView rvLogistics;
    @BindView(R.id.ll_loading_no_data)
    LinearLayout llLoadingNoData;
    @BindView(R.id.rl_logistics)
    RelativeLayout rlLogistics;
    @BindView(R.id.tv_logistics_status)
    TextView tvLogisticsStatus;
    @BindView(R.id.iv_no_data)
    ImageView ivNoData;
    @BindView(R.id.tv_no_data)
    TextView tvNoData;

    private LogisticsAdapter logisticsAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_logistics;
    }

    @Override
    protected void initView() {
        setCenterText("查看物流");

        mPresenter = new LogisticsPresenter();
        mPresenter.attachView(this);

        String orderNo = getIntent().getStringExtra("orderNo");
        String aaccessToken = SPUtils.getString("AaccessToken", "AaccessToken");

        mPresenter.getOrderLogistics(aaccessToken, orderNo);

        initCommentRv();

    }

    private void initCommentRv() {
        logisticsAdapter = new LogisticsAdapter(mContext, list);
        rvLogistics.setLayoutManager(new LinearLayoutManager(mContext));
        rvLogistics.setAdapter(logisticsAdapter);
//        commentAdapter.notifyDataSetChanged();
    }

    @Override
    public void noLogistics(String errMessage) {
//        showEmptyView("暂无物流信息");
    }

    List<LogisticsData.Result.Order> list = new ArrayList<>();

    @Override
    public void getOrderLogistics(LogisticsBean bean) {
        rlLogistics.setVisibility(View.VISIBLE);
//        bean.
        if (bean.getData() != null) {

            String datas = bean.getData().toString().replaceAll(" \" ", "");
            LogUtils.e("" + datas);

            LogisticsData logisticsData = new Gson().fromJson(datas, LogisticsData.class);

            RequestOptions options = new RequestOptions()
                    .placeholder(R.color.gray)
                    .error(R.color.gray);
            Glide.with(mContext).load(logisticsData.getResult().getLogo()).apply(options).into(ivOrderGoodsImg);

            tvOrderNo.setText(logisticsData.getResult().getNumber());


            //"快递收件(揽件)", @"在途中", @"正在派件", @"已签收", @"派送失败", @"疑难件", @"退件签收"
            switch (logisticsData.getResult().getDeliverystatus()) {
                case 0:
                    tvLogisticsStatus.setText("快递收件(揽件)");
                    break;
                case 1:
                    tvLogisticsStatus.setText("在途中");
                    break;
                case 2:
                    tvLogisticsStatus.setText("正在派件");
                    break;
                case 3:
                    tvLogisticsStatus.setText("已签收");
                    break;
                case 4:
                    tvLogisticsStatus.setText("派送失败");
                    break;
                case 5:
                    tvLogisticsStatus.setText("疑难件");
                    break;
                case 6:
                    tvLogisticsStatus.setText("退件签收");
                    break;

            }

            if (logisticsData.getResult().getExpName() == null) {
                tvOrderFirm.setText("(无)");
            } else {
                tvOrderFirm.setText(logisticsData.getResult().getExpName());
            }
            if (logisticsData.getResult().getList().size() > 0) {
                llLoadingNoData.setVisibility(View.GONE);
                for (int i = 0; i < logisticsData.getResult().getList().size(); i++) {
                    //添加集合中banner的数据
                    list.add(logisticsData.getResult().getList().get(i));
                }
            } else {
                if (list.isEmpty()) {
                    tvNoData.setText("暂无物流信息");
                    llLoadingNoData.setVisibility(View.VISIBLE);
//                showEmptyView("暂无数据");
                }
            }
            logisticsAdapter.notifyDataSetChanged();
        }


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
